<?php
/**
 * Created by PhpStorm .
 * User: {Administrator}
 * Date: {2022/7/8 15:37}
 * Author: '树叶'
 * Email: 'ayunu@qq.com'
 * Motto Info: '理想如果不向现实做一点点屈服，那么理想也将归于尘土。'
 */

namespace app\api\model;
use think\Db;
use think\Model;

class GoodsItem extends Model
{
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [

    ];

    /**
     * 获取规格键和值
     */
    public function getGoodsSpec($gid)
    {
        $goodsSpec = Db::name('goods_spec')->field('id,name')->where('goods_id',$gid)->select();

        if (!$goodsSpec) return false;

        foreach ($goodsSpec as $k => $v)
        {
            $goodsSpec[$k]['value'] = Db::name('goods_spec_value')->field('id,value')->where('goods_id',$gid)->where('spec_id',$v['id'])->select();
        }
        return $goodsSpec;
    }
}